package com.weimei.conversionTreasure.viewModel;

import android.app.Activity;
import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.weimei.conversionTreasure.activity.VipActivity;
import com.weimei.conversionTreasure.adapter.GDKgAdapter;
import com.weimei.conversionTreasure.adapter.GDTjAdapter;
import com.weimei.conversionTreasure.http.Repository;
import com.weimei.conversionTreasure.http.entity.KgjsBean;
import com.weimei.conversionTreasure.utils.UserUtils;
import com.weimei.conversionTreasure.utils.ValidateUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class GDJSVM extends BaseViewModel<Repository> {
    public GDJSVM(@NonNull Application application, Repository model) {
        super(application, model);
    }

    public List<GDKgAdapter.KgBean> BkgList = new ArrayList<>();
    public List<GDTjAdapter.TjBean> tjList = new ArrayList<>();
    public ObservableField<String> type = new ObservableField<>("0");


    public ObservableField<String> targetAlcoholDegree = new ObservableField<>();
    public ObservableField<String> targetMass = new ObservableField<>();
    public ObservableField<String> baseAlcoholXDegree = new ObservableField<>();
    public ObservableField<String> baseAlcoholXPrice = new ObservableField<>();

    public ObservableField<String> mbkg = new ObservableField<>();
    public ObservableField<String> mbtj = new ObservableField<>();
    public ObservableField<String> mbdj = new ObservableField<>();
    public ObservableField<String> jkg = new ObservableField<>();
    public ObservableField<String> jtj = new ObservableField<>();
    public ObservableField<String> pureWaterMass = new ObservableField<>();
    public ObservableField<String> pureWaterVolume = new ObservableField<>();

    public ObservableField<String> result = new ObservableField<>();


    public ObservableField<String> targetVolume = new ObservableField<>();

    public BindingCommand js = new BindingCommand(() -> {
        if (UserUtils.getFreeNum() > 0){
            if (type.get().equals("0")) {
                jsKg();
            } else {
                jsTj();
            }
        }else {
            if (UserUtils.isVip()) {
                if (type.get().equals("0")) {
                    jsKg();
                } else {
                    jsTj();
                }
            } else {
                ToastUtils.showShort("您还不是会员，请先开通会员后再使用！！！");
                startActivity(VipActivity.class);
            }
        }



    });

    public BindingCommand add = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (type.get().equals("0")) {
                if (kgAdapter.getData().size() > 9) {
                    ToastUtils.showShort("最多添加10类基酒");
                    return;
                }
                GDKgAdapter.KgBean kgBean = new GDKgAdapter.KgBean();

                kgAdapter.addData(kgBean);
            } else {
                if (tjAdapter.getData().size() > 9) {
                    ToastUtils.showShort("最多添加10类基酒");
                    return;
                }
                GDTjAdapter.TjBean tjBean = new GDTjAdapter.TjBean();
                tjAdapter.addData(tjBean);
            }
        }
    });

    public BindingCommand remove = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (type.get().equals("0")) {
                if (kgAdapter.getData().size() > 1) {
                    GDKgAdapter.KgBean kgBean = kgAdapter.getData().get(kgAdapter.getData().size() - 1);
                    kgAdapter.remove(kgBean);
                }
            } else {
                if (tjAdapter.getData().size() > 1) {
                    GDTjAdapter.TjBean tjBean = tjAdapter.getData().get(tjAdapter.getData().size() - 1);
                    tjAdapter.remove(tjBean);
                }
            }
        }
    });


    private void jsKg() {
        if (ValidateUtils.isEmptyString(targetAlcoholDegree.get())) {
            ToastUtils.showShort("请输入酒度");
            return;
        }

        if (ValidateUtils.isEmptyString(targetMass.get())) {
            ToastUtils.showShort("请输入质量");
            return;
        }
        if (ValidateUtils.isEmptyString(baseAlcoholXDegree.get())) {
            ToastUtils.showShort("请输入基酒x酒度");
            return;
        }

        if (ValidateUtils.isEmptyString(baseAlcoholXPrice.get())) {
            ToastUtils.showShort("请输入基酒x单价");
            return;
        }

        for (GDKgAdapter.KgBean bean : kgAdapter.getData()) {
            if (ValidateUtils.isEmptyString(bean.getDegree())) {
                ToastUtils.showShort("请输入基酒信息");
                return;
            }
        }
        String wineJson = new Gson().toJson(kgAdapter.getData());
        Log.d("XXXX", wineJson);

        Map<String, String> map = new HashMap<>();
        map.put("targetAlcoholDegree", targetAlcoholDegree.get());
        map.put("targetWineMass", targetMass.get());
        map.put("baseAlcoholXDegree", baseAlcoholXDegree.get());
        map.put("baseAlcoholXPrice", baseAlcoholXPrice.get());
        map.put("wineJson", wineJson);
        model.blending_weight(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {
                })
                .subscribe(new DisposableObserver<KgjsBean>() {
                    @Override
                    public void onNext(KgjsBean kgjsBean) {
                        if (kgjsBean.issucc) {
                            UserUtils.opreFree();
                            mbtj.set(kgjsBean.getData().getTargetWineVolume());
                            mbdj.set(kgjsBean.getData().getTargetWinePrice());
                            jkg.set(kgjsBean.getData().getBaseAlcoholXMass());
                            jtj.set(kgjsBean.getData().getBaseAlcoholXVolume());
                            pureWaterMass.set(kgjsBean.getData().getPureWaterMass());
                            pureWaterVolume.set(kgjsBean.getData().getPureWaterVolume());

                            StringBuilder sb = new StringBuilder();

                            for (int i = 0; i < kgjsBean.getData().getYVolumeList().size(); i++) {
                                sb.append("\n基酒" +  (i+1) + "体积：" + kgjsBean.getData().getYVolumeList().get(i).getBaseAlcoholYVolume());
                            }

                            String s = sb.toString();
                            result.set("结果：" + s);

                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void jsTj() {
        if (ValidateUtils.isEmptyString(targetAlcoholDegree.get())) {
            ToastUtils.showShort("请输入酒度");
            return;
        }

        if (ValidateUtils.isEmptyString(targetVolume.get())) {
            ToastUtils.showShort("请输入体积");
            return;
        }
        if (ValidateUtils.isEmptyString(baseAlcoholXDegree.get())) {
            ToastUtils.showShort("请输入基酒x酒度");
            return;
        }
        if (ValidateUtils.isEmptyString(baseAlcoholXPrice.get())) {
            ToastUtils.showShort("请输入基酒x单价");
            return;
        }


        for (GDTjAdapter.TjBean bean : tjAdapter.getData()) {
            if (ValidateUtils.isEmptyString(bean.getDegree())) {
                ToastUtils.showShort("请输入基酒信息");
                return;
            }
        }
        String wineJson = new Gson().toJson(tjAdapter.getData());
        Log.d("XXXX", wineJson);

        Map<String, String> map = new HashMap<>();
        map.put("targetAlcoholDegree", targetAlcoholDegree.get());
        map.put("targetVolume", targetVolume.get());
        map.put("baseAlcoholXDegree", baseAlcoholXDegree.get());
        map.put("baseAlcoholXPrice", baseAlcoholXPrice.get());
        map.put("wineJson", wineJson);
        model.blending_volume(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {
                })
                .subscribe(new DisposableObserver<KgjsBean>() {
                    @Override
                    public void onNext(KgjsBean kgjsBean) {
                        if (kgjsBean.issucc) {
                            UserUtils.opreFree();
                            mbtj.set(kgjsBean.getData().getTargetWineVolume());
                            mbdj.set(kgjsBean.getData().getTargetWinePrice());
                            jkg.set(kgjsBean.getData().getBaseAlcoholXMass());
                            jtj.set(kgjsBean.getData().getBaseAlcoholXVolume());
                            pureWaterMass.set(kgjsBean.getData().getPureWaterMass());
                            pureWaterVolume.set(kgjsBean.getData().getPureWaterVolume());
                            StringBuilder sb = new StringBuilder();

                            for (int i = 0; i < kgjsBean.getData().getYVolumeList().size(); i++) {
                                sb.append("\n基酒" + (i+1) + "重量：" + kgjsBean.getData().getYVolumeList().get(i).getBaseAlcoholYVolume());
                            }

                            String s = sb.toString();
                            result.set("结果：" + s);

                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    GDKgAdapter kgAdapter;
    GDTjAdapter tjAdapter;

    RecyclerView mKgRecy, mTjRecy;
    Activity mActivity;

    @Override
    public void setActivity(Activity activity) {
        super.setActivity(activity);
        this.mActivity = activity;
    }

    public void setView(RecyclerView kgRecy, RecyclerView tjRecy) {
        mKgRecy = kgRecy;
        mTjRecy = tjRecy;

        mKgRecy.setLayoutManager(new LinearLayoutManager(mActivity) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });

        mTjRecy.setLayoutManager(new LinearLayoutManager(mActivity) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });

        kgAdapter = new GDKgAdapter();
        mKgRecy.setAdapter(kgAdapter);
        GDKgAdapter.KgBean kgBean = new GDKgAdapter.KgBean();
        kgAdapter.addData(kgBean);

        tjAdapter = new GDTjAdapter();
        mTjRecy.setAdapter(tjAdapter);
        GDTjAdapter.TjBean tjBean = new GDTjAdapter.TjBean();
        tjAdapter.addData(tjBean);


    }

    public void change() {
        targetAlcoholDegree.set("");
        targetMass.set("");
        baseAlcoholXDegree.set("");
        baseAlcoholXPrice.set("");

        mbkg.set("");
        mbtj.set("");
        mbdj.set("");
        jkg.set("");
        jtj.set("");
        pureWaterMass.set("");
        pureWaterVolume.set("");

        result.set("");
        targetVolume.set("");

    }
}
